package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.HangYeFeiLeiAdapter;
import com.tangkun.environment.adapter.ZiXunFeiLeiAdapter;
import com.tangkun.environment.entity.gongzuo.CategoryTreeDataReturn;
import com.tangkun.environment.entity.xinwen.TypeReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;
import com.tangkun.environment.view.CustomListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 行业分类
 */

public class HangYeFenLeiActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;

    /**
     * 数据源 行业分类
     */
    private List<CategoryTreeDataReturn.DataBean> dataList = new ArrayList<CategoryTreeDataReturn.DataBean>();
    /**
     * 适配器
     */
    private HangYeFeiLeiAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_customlistview);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mContext = this;
        dataList = (List<CategoryTreeDataReturn.DataBean>) getIntent().getSerializableExtra("data");
        title.setText("行业分类");

        adapter = new HangYeFeiLeiAdapter(HangYeFenLeiActivity.this, dataList);
        customListView.setAdapter(adapter);
        customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (dataList.get(position - 1).children == null || dataList.get(position - 1).children.size() <= 0) {//已经是最后一层
                    //选中的ID和名字
                    String ids = dataList.get(position - 1).id;
                    String name = dataList.get(position - 1).name;
                    //成功后调用下面代码
                    Intent intent = new Intent();
                    intent.putExtra("name", name);
                    intent.putExtra("id", ids);
                    setResult(RESULT_OK, intent);
                    Utils.hideSoftKeyboard(HangYeFenLeiActivity.this, back);
                    JumpHelper.finish(mContext);
                } else {//不是最后一层重新做拼接
                    List<CategoryTreeDataReturn.DataBean.ChildrenBean> dataListChildren = dataList.get(position - 1).children;
                    dataList.clear();
                    for (CategoryTreeDataReturn.DataBean.ChildrenBean childrenBean : dataListChildren) {
                        CategoryTreeDataReturn.DataBean bean = new CategoryTreeDataReturn.DataBean();
                        bean.ord = childrenBean.ord;
                        bean.isParent = childrenBean.isParent;
                        bean.address = childrenBean.address;
                        bean.createTime = childrenBean.createTime;
                        bean.icon = childrenBean.icon;
                        bean.name = childrenBean.name;
                        bean.description = childrenBean.description;
                        bean.id = childrenBean.id;
                        bean.tasknum = childrenBean.tasknum;
                        bean.parentId = childrenBean.parentId;
                        dataList.add(bean);
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }


    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back://返回按钮
                JumpHelper.finish(mContext);
                break;
        }
    }
}
